package net.majodali.glasshouse;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import net.majodali.glasshouse.filesystem.FileResource;
import net.majodali.glasshouse.generators.Generator;
import net.majodali.glasshouse.generators.GeneratorRule;

abstract public class AbstractProject {
	private Collection<ResourceMatcher> inputResources = new ArrayList<ResourceMatcher>();
	private Collection<Rule> rules = new ArrayList<Rule>();
	Map<String, Resource> resources = new HashMap<String, Resource>();
	
	public Resource getResource(String path) {
		Resource r = resources.get(path);
		if (r == null)
			resources.put(path, r = createResource(path));
		return r;
	}

	public void build() {
		for (Rule rule : rules) {
			for (Resource resource : findResources(rule.getInputMatcher())) {
				rule.getOutputResources(resource);
			}
		}
	}
	
	public Collection<Resource> findResources(ResourceMatcher matcher) {
		throw new RuntimeException("AbstractProject.findResources() not implemented");
	}

	abstract public Iterable<Resource> allResources();

	public AbstractProject inputResource(ResourceMatcher resourceMatcher) {
		inputResources.add(resourceMatcher);
		return this;
	}
	
	public AbstractProject inputResource(String path) {
		return inputResource(new ResourceNameMatcher(path));
	}
	
	public AbstractProject rule(Rule rule) {
		rules.add(rule);
		return this;
	}
	
	public AbstractProject compile(String source, String targetPath) {
		throw new RuntimeException("AbstractProject.compile() not implemented");
	}
	
	public AbstractProject generate(Generator generator, String source, String targetPath) {
		throw new RuntimeException("AbstractProject.findResources() not implemented");
	}
	
	abstract protected Resource createResource(String path);
}
